﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Service.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.BasicDataManage
{
    /// <summary>
    /// 异常分类管理业务层
    /// </summary>
    public class ETypeDetailBusiness
    {
        private readonly ETypeDetailService _dal = new ETypeDetailService();

        /// <summary>
        /// 添加异常分类管理
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool AddETypeDetail(string data)
        {
            var model = new ETypeDetailEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.DetailID = Guid.NewGuid();
                model.CatID = new Guid(row["CatID"].ToString());
                model.DetailCode = row["DetailCode"].ToString();
                model.DetailName = row["DetailName"].ToString();
                model.DetailDesc = row["DetailDesc"] != null ? row["DetailDesc"].ToString() : "";
            }
            return _dal.AddETypeDetail(model);
        }

        /// <summary>
        /// 修改异常分类管理
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool ModifyETypeDetail(string data)
        {
            var model = new ETypeDetailEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.DetailID = new Guid(row["DetailID"].ToString());
                model.DetailCode = row["DetailCode"].ToString();
                model.DetailName = row["DetailName"].ToString();
                model.DetailDesc = row["DetailDesc"] != null ? row["DetailDesc"].ToString() : "";
            }
            return _dal.ModifyETypeDetail(model);
        }

        /// <summary>
        /// 获取异常分类管理单条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetETypeDetailInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetETypeDetailInfo(id));
        }

        /// <summary>
        /// 获取异常分类管理分页
        /// </summary>
        /// <returns></returns>
        public string GetETypeDetailList(ETypeDetailQcEntity qcmodel)
        {
            var total = _dal.GetETypeDetailCount(qcmodel);
            var list = _dal.GetETypeDetailList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public void ImportETypeDetail(string servicePath, string data)
        {
            var rows = (ArrayList)JsonHelper.Decode(data);
            var model = new ETypeDetailEntity();
            var excelPath = "";
            foreach (Hashtable row in rows)
            {
                excelPath = row["excUrl"].ToString();
                model.CatID = new Guid(row["CatID"].ToString());
            }
            var paht = servicePath + excelPath;
            var dt = ExcelHelper.Import(paht);
            var list = _dal.GetETypeDetailList(model.CatID.ToString());
            var excelList = GetList(dt, model.CatID);
            var updateList = new List<ETypeDetailEntity>();
            var addList = new List<ETypeDetailEntity>();
            if (list.Count > 0)
            {
                foreach (var i in excelList)
                {
                    var q = list.Where(T => T.DetailCode.Equals(i.DetailCode));
                    if (q.Count() > 0)
                    {
                        var detailId = q.FirstOrDefault().DetailID;
                        i.DetailID = detailId;
                        updateList.Add(i);
                    }
                    else
                    {
                        addList.Add(i);
                    }
                }
                if (addList.Count > 0)
                {
                    _dal.ImportETypeDetail(addList);
                }
                if (updateList.Count > 0)
                {
                    _dal.UpdateETypeDetail(updateList);
                }


                //var newlist =
                //    list.Union(excelList, new MyComparer()).Except(list.Intersect(excelList, new MyComparer()),
                //                                                   new MyComparer()).ToList();
                //if (newlist.Count > 0)
                //{
                //    _dal.ImportETypeDetail(newlist);
                //}
            }
            else
            {
                //直接导入。
                _dal.ImportETypeDetail(excelList);
            }
        }

        private static List<ETypeDetailEntity> GetList(DataTable dt, Guid id)
        {
            try
            {
                var dr = dt.Select();
                return (from t in dr
                        select new ETypeDetailEntity
                        {
                            DetailID = Guid.NewGuid(),
                            CatID = id,
                            DetailCode = t["编号"].ToString().Trim().Replace("<空>", ""),
                            DetailName = t["名称"].ToString().Trim().Replace("<空>", ""),
                            DetailDesc = t["备注"].ToString().Trim().Replace("<空>", ""),
                            RecStatus = true
                        }).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable ExportETypeDetail(ETypeDetailQcEntity qcmodel)
        {
            var list = _dal.ExportETypeDetail(qcmodel);
            return Collection.ListToDataTable(list);
        }
    }
}
